package com.iap.admin.view.tag.code
{
	import com.iap.common.signal.TagRequestSignal;
	import com.iap.admin.view.code.EditViewCode;
	import com.iap.admin.view.tag.TagEditView;
	import com.iap.app.util.FormUtils;
	import com.iap.common.model.Tag;
	
	import flash.events.Event;
	
	import mx.validators.StringValidator;
	
	public class TagEditViewCode extends EditViewCode
	{
		public static const CANCEL_NAME:String = "TagEdit.Cancel";
		public static const SAVE_NAME:String = "TagEdit.Save";
		public static const DELETE_NAME:String = "TagEdit.Delete";
		
		public var onSubmit:TagRequestSignal = new TagRequestSignal();
		
		private var nameValidator:StringValidator;
		private var view:TagEditView;
		
		public function TagEditViewCode()
		{
			super();
		}
		
		protected function initView(view:TagEditView):void
		{
			this.view = view;
			addListener();
			initValidators();
			resetFocus();
			onReady.dispatch("ready");
		}
		
		private function initValidators():void
		{
			nameValidator = new StringValidator();
			
			nameValidator.source = view.nameForm;
			nameValidator.property = "text";
			nameValidator.required = true;
			nameValidator.maxLength = 100;
			
			validateData.push(nameValidator);
		}
		
		private function addListener():void
		{
			view.nameForm.addEventListener(Event.CHANGE, validateForm);
		}
		
		private function removeListener():void
		{
			view.nameForm.removeEventListener(Event.CHANGE, validateForm);
		}
		
		private function resetFocus():void
		{
			view.focusManager.setFocus(view.nameForm);
		}
		
		protected function submit():void 
		{
			model ||= new Tag();
			model = FormUtils.fillObjectFromControls(view, model, FORM_PREFIX);
			onSubmit.dispatch(model, TagRequestSignal.SAVE);
		}
		
		protected function remove():void 
		{
			onSubmit.dispatch(model, TagRequestSignal.DELETE);
		}
	}
}